package fr.grassaud.hmvc.session;

import java.util.ArrayList;
import java.util.List;

public class HMVCSessionFactory {

    private Class<?> rootControllerClass;

    private List<TriadListener> listeners;


    /**
     * Creates a new session for the current local thread
     * 
     * @return the session
     */
    public HMVCSession newSession() {
	if (rootControllerClass == null) {
	    throw new IllegalStateException("the root controller should be set to create a session");

	}
	
	return createNewSession();
    }

    private HMVCSession createNewSession() {
	HMVCSessionImpl session = new HMVCSessionImpl();
	if (this.listeners != null) {
	    for (TriadListener listener : listeners) {
		session.addTriadListener(listener);
	    }
	}
	session.setRoot(rootControllerClass);

	
	return session;
    }

    public Class<?> getRootControllerClass() {
	return rootControllerClass;
    }

    public void setRootControllerClass(Class<?> rootControllerClass) {
	this.rootControllerClass = rootControllerClass;
    }

   
    public void addTriadListener(TriadListener listener) {
	if (this.listeners == null) {
	    this.listeners = new ArrayList<TriadListener>();
	}
	this.listeners.add(listener);
    }

   

   
}
